<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require(APPPATH . 'core/admin.php');

class Profile extends Admin
{
    public function getPage()
    {
        return 'profile';
    }

    public function getMoreData() {
        $sql = "SELECT sLogin, users.sName, users.sAddress, users.sPhone, users.sEmail, users.bErrorSynced, groups.sName AS sGroupName
                    FROM users LEFT JOIN groups ON groups.nId = users.nGroupId WHERE users.nId = ?";
        $query = $this->db->query($sql, array($this->session->userdata('userId')));

        if ($query->num_rows() > 0) {
            $row = $query->row();

            $data['login'] = $row->sLogin;
            $data['name'] = $row->sName;
            $data['address'] = $row->sAddress;
            $data['phone'] = $row->sPhone;
            $data['email'] = $row->sEmail;
            $data['group'] = $row->sGroupName;
            $data['errorSynced'] = $row->bErrorSynced;

            return $data;
        }
        return array();
    }

    public function save()
    {
        $data = array(
            'sName' => $_POST['name'],
            'sAddress' => $_POST['address'],
            'sPhone' => $_POST['phone'],
            'sEmail' => $_POST['email'],
            'bErrorSynced' =>  intval($_POST['errorSynced'])
        );

        $this->db->where('nId', $this->session->userdata('userId'));
        $this->db->update('users', $data);

        redirect(site_url('profile/index/common.ok'));
    }
}